#include"all.hpp"

using namespace std;

//管理管道文件
int main()
{
    //创建信道
    int n = mkfifo(FIFO_FILE, MODE);
    if(n == -1)
    {
        // printf("%d: %s\n", errno, sterror(errno));
        perror("mkfifo\n");
        exit(FIFO_CREAT_ERR);
    }


    //打开信道
    int fd = open(FIFO_FILE, O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        exit(FIFO_OPEN_ERR);
    }

    cout<<"server open file done"<<endl;
    //client打开后才打印，说明open阻塞了
    //开始通信
    while(true)
    {
        char buffer[1024];
        int x = read(fd, buffer,sizeof(buffer));
        if(x > 0)
        {
            buffer[x] = 0;
            cout<<"client :"<<buffer<<endl;
        }
        else if(x == 0) 
        {
            cout<<"client quit,me too"<<endl;
            break;
        }
        else break;
    }


    close(fd);

    int m = unlink(FIFO_FILE);
    if(m == -1)
    {
        perror("unlink");
        exit(FIFO_DELETE_ERR);
    }
    return 0;
}